<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>截图粘贴演示</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 30px;
            color: #333;
        }
        
        .container {
            max-width: 900px;
            width: 100%;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin-top: 20px;
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 2.2rem;
        }
        
        .description {
            text-align: center;
            color: #7f8c8d;
            margin-bottom: 30px;
            font-size: 1.1rem;
            line-height: 1.6;
        }
        
        .paste-area {
            border: 3px dashed #3498db;
            border-radius: 12px;
            padding: 40px;
            text-align: center;
            margin-bottom: 30px;
            transition: all 0.3s;
            background: #f8fafc;
        }
        
        .paste-area:hover, .paste-area.dragover {
            background: #e1f0fa;
            border-color: #2980b9;
        }
        
        .paste-area i {
            font-size: 50px;
            color: #3498db;
            margin-bottom: 15px;
        }
        
        .paste-area p {
            font-size: 18px;
            color: #7f8c8d;
            margin: 10px 0;
        }
        
        .key-shortcut {
            display: inline-block;
            background: #eef2f7;
            padding: 5px 12px;
            border-radius: 4px;
            font-weight: bold;
            color: #2c3e50;
            margin: 0 5px;
        }
        
        .image-preview {
            display: none;
            margin-top: 30px;
            text-align: center;
        }
        
        .image-preview img {
            max-width: 100%;
            max-height: 400px;
            border-radius: 8px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            border: 1px solid #ddd;
        }
        
        .button {
            background: #3498db;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
            margin: 10px 5px;
            display: inline-block;
        }
        
        .button:hover {
            background: #2980b9;
        }
        
        .button-secondary {
            background: #2ecc71;
        }
        
        .button-secondary:hover {
            background: #27ae60;
        }
        
        .instructions {
            background: #f8f9fa;
            border-left: 4px solid #3498db;
            padding: 20px;
            margin: 30px 0;
            border-radius: 0 8px 8px 0;
        }
        
        .instructions h3 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .instructions ol {
            padding-left: 20px;
            line-height: 1.8;
        }
        
        .footer {
            margin-top: 30px;
            text-align: center;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 20px;
            }
            
            .paste-area {
                padding: 20px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
    <h1>网页截图粘贴演示</h1>
    <p class="description">尝试使用快捷键截图后，在此页面粘贴（Ctrl+V），或直接将图片文件拖放到下方区域</p>
    
    <div class="container">
        <div class="image-preview" id="imagePreview">
            <h2>粘贴的图片预览</h2>
            <img id="previewImg" src="" alt="预览图片">
            <div class="actions">
                <a id="downloadBtn" class="button button-secondary" download="screenshot.png">下载图片</a>
                <button id="clearBtn" class="button">清除图片</button>
                <button id="recognizeText" class="button">文字识别</button>
            </div>
        </div>
        <div class="paste-area" id="pasteArea">
            <i>📋</i>
            <p>使用 <span class="key-shortcut">Ctrl+V</span> 粘贴截图</p>
            <p>或直接将图片文件拖放到此区域</p>
        </div>
        
        <textarea id="result" class="paste-area" style="width: 100%;height: 100px;" placeholder="识别结果"></textarea>
        <div class="instructions">
            <h3>如何截图并粘贴</h3>
            <ol>
                <li>使用截图工具截取屏幕（全屏、区域或应用窗口）</li>
                <li>Windows: 按 <span class="key-shortcut">PrtScn</span> 或 <span class="key-shortcut">Win+Shift+S</span></li>
                <li>Mac: 按 <span class="key-shortcut">Cmd+Shift+4</span></li>
                <li>截图后，点击此页面并按 <span class="key-shortcut">Ctrl+V</span> (Windows) 或 <span class="key-shortcut">Cmd+V</span> (Mac)</li>
                <li>或者直接将图片文件拖放到上方区域</li>
            </ol>
        </div>
    </div>
    
    <div class="footer">
        <p>此示例在现代浏览器（Chrome、Edge、Firefox、Safari）中均可工作</p>
    </div>

    <script>

        document.addEventListener('DOMContentLoaded', function() {
            const pasteArea = document.getElementById('pasteArea');
            const imagePreview = document.getElementById('imagePreview');
            const previewImg = document.getElementById('previewImg');
            const downloadBtn = document.getElementById('downloadBtn');
            const clearBtn = document.getElementById('clearBtn');
            const recognizeText = document.getElementById('recognizeText');
            // 处理粘贴事件
            document.addEventListener('paste', function(e) {
                // 检查剪贴板中是否包含图片
                if (e.clipboardData && e.clipboardData.items) {
                    const items = e.clipboardData.items;
                    
                    for (let i = 0; i < items.length; i++) {
                        if (items[i].type.indexOf('image') !== -1) {
                            const blob = items[i].getAsFile();
                            const url = URL.createObjectURL(blob);
                            
                            // 显示图片
                            previewImg.src = url;
                            imagePreview.style.display = 'block';
                            
                            // 设置下载链接
                            downloadBtn.href = url;
                            
                            // 滚动到图片位置
                            imagePreview.scrollIntoView({ behavior: 'smooth' });
                            
                            break;
                        }
                    }
                }
            });
            
            // 处理拖放事件
            pasteArea.addEventListener('dragover', function(e) {
                e.preventDefault();
                pasteArea.classList.add('dragover');
            });
            
            pasteArea.addEventListener('dragleave', function() {
                pasteArea.classList.remove('dragover');
            });
            
            pasteArea.addEventListener('drop', function(e) {
                e.preventDefault();
                pasteArea.classList.remove('dragover');
                
                const files = e.dataTransfer.files;
                if (files.length) {
                    const file = files[0];
                    
                    if (file.type.indexOf('image') !== -1) {
                        const url = URL.createObjectURL(file);
                        
                        // 显示图片
                        previewImg.src = url;
                        imagePreview.style.display = 'block';
                        
                        // 设置下载链接
                        downloadBtn.href = url;
                        
                        // 滚动到图片位置
                        imagePreview.scrollIntoView({ behavior: 'smooth' });
                    }
                }
            });
            
            // 清除图片
            clearBtn.addEventListener('click', function() {
                previewImg.src = '';
                imagePreview.style.display = 'none';
            });
            
            // 识别文字
            recognizeText.addEventListener('click', uploadFile);
            
            // 点击粘贴区域触发粘贴事件
            pasteArea.addEventListener('click', function() {
                alert('请使用 Ctrl+V (Windows) 或 Cmd+V (Mac) 粘贴截图');
            });
        });

        // 处理文件上传
        async function uploadFile() {
            const img = document.getElementById('previewImg');
            const response = await fetch(img.src);
            const blob = await response.blob();
            
            const formData = new FormData();
            formData.append('file', blob);
            try {
                const response = await fetch('/api/ocr/', {
                    method: 'POST',
                    body: formData
                });
                const data = await response.json();
                showResults(data);
            } catch (error) {
                alert('识别失败: ' + error.message);
            }
        }

        // 显示结果
        function showResults(data) {
            console.log(data);
            const resultDiv = document.getElementById('result');
            if (data.status !== 'success') {
                resultDiv.value = `错误: ${data.detail}`;
                return;
            }
            resultDiv.value = data.results.join("\n");
        }
    </script>
</body>
</html>